<?php
/**
 * @description : auto authorized the request token
 * @author      : stephen.mo
 * @date        : Oct 8,2010
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set('display_errors','on');

#get all vars from params.
$callback_url = (isset($_REQUEST['oauth_callback'])) ? $_REQUEST['oauth_callback'] : NULL;
$oauth_token  = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL;
$pccw_token   = (isset($_REQUEST['token'])) ? $_REQUEST['token'] : NULL;
$lang         = (isset($_REQUEST['lang'])) ? OAuthUtils::chkLang($_REQUEST['lang']) : OAuthConfig::DEFAULT_SUPPORT_LANG;

//neet to use callback url to get oauth_token.
if (empty($callback_url) || empty($oauth_token) || empty($pccw_token)) {
    $other_msg  = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1032),$other_msg);
    exit;
}

//setting the vars.
$base_server_url  = OAuthConfig::OAuthDomainUrl($_SERVER);
$message_url      = $base_server_url.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;
$default_header   = $base_server_url.OAuthConfig::DEFAULT_HEADER_URL;
$authorize_url    = $base_server_url.OAuthConfig::DEFAULT_AUTHORIZE_URL;
$retry_url        = $base_server_url.OAuthConfig::DEFAULT_THIRD_PARTIES_URL;
$base_back_string = '?oauth_token='.$oauth_token.'&oauth_callback='.urlencode($callback_url).'&lang='.$lang;
$cancel_url       = $authorize_url.$base_back_string;
$retry_url        = $retry_url.$base_back_string;

//getting pccw's username and other informations.
$now_time  = time();
$pccw_obj  = OAuthUtils::DecryptPccwString($pccw_token);
//$username  = (isset($pccw_obj->NOWID)) ? $pccw_obj->NOWID : NULL;
$username  = (isset($pccw_obj->MASKEDNPID)) ? $pccw_obj->MASKEDNPID : NULL;
$timestamp = (isset($pccw_obj->TIMESTAMP)) ? $pccw_obj->TIMESTAMP : 0;
if (OAuthModelConfig::OAUTH_NEED_CHKTIMESTAMP) {
    if (($now_time - $timestamp) > (OAuthModelConfig::OAUTH_PCCW_TIMESTAMP_EXPIRY*60)) {
        $other_msg  = array('call_back'=>urlencode($retry_url));
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1032),$other_msg);
        exit;
    }
}

//get ms's login_id by pccw_user_id.
$password  = sha1($timestamp.$username);
$username  = OAuthUtils::getLoginIdByUserId($username);
if (empty($pccw_obj) || empty($username)) {
    $other_msg  = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1032),$other_msg);
    exit;
}

//auto login.
$rev = OAuthUtils::loginForMobile($username,$password,OAuthModelConfig::PCCW_OAUTH_FROM_TYPE);
if (!$rev) {
    $other_msg  = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1004),$other_msg);   
    exit;
}
$login_token   = $rev->token;
$phone_imei    = NULL;
$request_token = (isset($_REQUEST['oauth_token'])) ? $_REQUEST['oauth_token'] : NULL; 
$consumer_key  = OAuthUtils::getConsumerKeyByToken($request_token);
if (empty($consumer_key) || empty($request_token)) {
    $other_msg  = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1005),$other_msg);      
    exit;
}

//authorize the request token and return authorized token.
$request_vars = array();
$request_vars['consumer_key']  = $consumer_key;
$request_vars['request_token'] = $request_token;
$request_vars['user_name']     = $username;
$authorize_result = OAuthUtils::authorizedRequestToken($request_vars);
if (empty($authorize_result) || !$authorize_result) {
    $other_msg  = array('call_back'=>urlencode($retry_url));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1006),$other_msg);         
    exit;
}

//setting the cookie vars.
$params = array();
$params['rand_code']      = $authorize_result['oauth_verify'];
$params['request_token']  = $request_token;
$params['request_secret'] = $authorize_result['secret'];
$params['user']           = $username;
$params['login_id']       = $username;
$params['phone_imei']     = $phone_imei;
$params['token']          = $login_token;
$params['from']           = OAuthModelConfig::PCCW_OAUTH_FROM_TYPE;
if (OAuthModelConfig::USING_MEM_SAVE_MEMBER) {
    OAuthUtils::setRequest($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
    OAuthUtils::setUserInfo($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
}

//save data to mc.
$rev = OAuthUtils::saveUserInfoToMc($params,OAuthModelConfig::SAVE_MEMBER_INFO_ETIME);
$rev = OAuthUtils::saveLoginInfo($username,$lang);

//return or output.
$output = 'oauth_token='.$authorize_result['key'].
          '&oauth_token_secret='.$authorize_result['secret'].
          '&oauth_verifier='.$authorize_result['oauth_verify'];
if (!empty($callback_url)) {
    $callback_url = (strpos($callback_url,'?')===false) ? $callback_url.'?'.$output : $callback_url.'&'.$output;
    Header("Location:$callback_url");
    exit;
}
    
//output the authorized token string.
echo $output;
exit;

?>
